//  Created by 莫瑞权 on 2022/5/5.
//  Copyright © 2022 Talkfun. All rights reserved.
//

import UIKit

class TFRoundBackground: UIButton {
    @objc public let name = UILabel()
    @objc public var redDotLayer:CAShapeLayer?
    @objc public let roundBackground = UIView()
    @objc public var fullScreen:Bool = false //不修改imageView
   
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        if (UIDevice.current.userInterfaceIdiom == .phone)
        {
            self.addSubview(self.roundBackground)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        
        if (self.fullScreen==false) {

            let imageViewW:CGFloat = self.frame.size.width*0.5
            let imageViewH:CGFloat = self.frame.size.height*0.5

            let imageViewX:CGFloat = ( self.frame.size.width-imageViewW)/2;
            let imageViewY:CGFloat = (self.frame.size.height-imageViewH)/2
            self.imageView!.frame = CGRect(x: imageViewX, y: imageViewY, width: imageViewW, height: imageViewH)
 
        }
    }
    //画小红点
 @objc public func drawRedDot()

    {
        
        if(self.redDotLayer==nil){
            self.clipsToBounds = false;
            
            self.redDotLayer = CAShapeLayer()
            // 指定frame，
            self.redDotLayer!.frame = CGRect(x: self.frame.size.width-9-4.5, y: 9/2, width:9 , height: 9)
        
            // 设置填充颜色
            self.redDotLayer!.fillColor = UIColor.red.cgColor
            // 设置线宽
            self.redDotLayer!.lineWidth = 1
            // 设置线的颜色
            self.redDotLayer!.strokeColor =  UIColor.red.cgColor
            // 使用UIBezierPath创建路径
            self.redDotLayer!.masksToBounds = true
            
            self.redDotLayer!.cornerRadius = self.redDotLayer!.frame.width/2;

            // 设置CAShapeLayer与UIBezierPath关联
          self.redDotLayer!.path = UIBezierPath.init(rect: CGRect(x: 0, y: 0, width: self.redDotLayer!.frame.size.width, height: self.redDotLayer!.frame.size.height)).cgPath
            
            self.redDotLayer!.strokeStart = 0.0
            self.redDotLayer!.strokeEnd =   1.0;
            // 将CAShaperLayer放到某个层上显示
            
        self.layer.addSublayer(self.redDotLayer!)
        }
            
            
    }
    //清除小红点
    @objc public func clearRedDot()
   {
   
     if  self.redDotLayer != nil {
         self.redDotLayer!.removeFromSuperlayer()
         self.redDotLayer = nil
     }
     

        
    }
   
}
